# # 任意定义⼀个函数给其添加装饰器
# # 要求：每次执⾏该函数 装饰器⾃动记录次数 记录需保存在⽂件中
# running_count = 0
#
#
# def decorator(func):
#     def inner(*args, **kwargs):
#         global running_count
#         running_count += 1
#         with open('running_count.txt', 'w', encoding='utf-8') as f:
#             f.write('函数的运行次数为 %s ' % running_count)
#         res = func(*args, **kwargs)
#         return res
#
#     return inner
#
#
# @decorator
# def index():
#     print("from index")
#
#
# index()


# 任意定义⼀个函数给其添加装饰器
# 要求：每次执⾏该函数 装饰器⾃动记录次数 记录需保存在⽂件中

def decorator(func):
    def inner(*args, **kwargs):
        with open('running_count.txt', 'r', encoding='utf-8') as f:
            running_count = int(f.readline().split(" ")[1])

        running_count += 1
        with open('running_count.txt', 'w', encoding='utf-8') as f:
            f.write('函数的运行次数为 %s' % running_count)
        res = func(*args, **kwargs)
        return res

    return inner


@decorator
def index():
    print("from index")


index()
